package org.top10.parser;

import java.util.ArrayList;
import java.util.List;

import org.top10.collector.Filter;
import org.top10.entity.CollectEntity;
import org.top10.entity.Finance;
import org.top10.entity.Forum;
import org.top10.executor.ParseSource;
import org.top10.util.NdcFunction;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class HexunTask extends Task{
	private static final int webSiteID = TOP10Global.WEB_SITE_ID_HEXUN;// 对应网站ID
	private static final String ENCoding =TOP10Global.Encoding_GDK;// 对应网站ID
	
	public HexunTask() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		PARSER.add(new HexunParser());
		PARSER.add(new HexunGubaParser());
	}
	public static void main(String arg[]) throws Exception{
		new HexunGubaParser().dealText(ENCoding);
		
	}
	
}
 class HexunParser extends TOPParserImpl {
	
	private static final String URL="http://fblog.hexun.com/";
	private static final String BaseUrl="http://top.baidu.com/";
	private static final String SIGN="Con2_2";
	public HexunParser() {
		super("资讯排行",TOP10Global.TYPE_Finance, URL);
	}
	public  List<CollectEntity> parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("body").item(0);
		//Node node = getNodeByText(body, "id",SIGN);
		Node node = getNodeByAttr("ul","id",SIGN,body);
		ArrayList<Node> as = new ArrayList<Node>();
		getAllTagsByParent(node, "a", as);
		List<CollectEntity> list = new ArrayList<CollectEntity>();
		for (int i = 0; i < as.size(); i++) {
			Node tr=as.get(i);
			CollectEntity v =build(tr,i+1);
			System.out.println(v);
			list.add(v);
			if(i>=9)break;
		}
		return list;
	}

	public  CollectEntity build(Node node, int rank) throws Exception {
		Finance v=new Finance();
		v.setRank(rank);
		v.setName(node.getTextContent());
		String url=getAttrValueByTag(node,"href");
		v.setLinkUrl(url);
		return v;
	}
}
 class HexunGubaParser extends TOPParserImpl {
		
		private static final String URL="http://guba.hexun.com/";
		private static final String SIGN="gtit_48";
		private static final String Filter_SIGN="f14";
		private final Filter filter=new Filter(){

			public boolean pass(Node node) {
				String value=getAttrValueByTag(node,"class");
				if(value!=null&&Filter_SIGN.equals(value)){
					return true;
				}
				return false;
			}
			
		};
		public HexunGubaParser() {
			super("股吧排行",TOP10Global.TYPE_Forum, URL);
		}
		public  List<CollectEntity> parse(ParseSource doc) throws Exception {
			Node body = doc.getDocSource().getElementsByTagName("body").item(0);
			//Node node = getNodeByText(body, "id",SIGN);
			Node node = getNodeByAttr("img","class",SIGN,body).getParentNode().getParentNode().getParentNode();
			ArrayList<Node> as = new ArrayList<Node>();
			getAllTagsByParent(node, "a", as,filter);
			List<CollectEntity> list = new ArrayList<CollectEntity>();
			for (int i = 0; i < as.size(); i++) {
				Node tr=as.get(i);
				CollectEntity v =build(tr,i+1);
				list.add(v);
				if(i>=9)break;
			}
			return list;
		}

		public  CollectEntity build(Node node, int rank) throws Exception {
			Forum v=new Forum();
			v.setRank(rank);
			v.setName(node.getTextContent());
			String url=getAttrValueByTag(node,"href");
			v.setLinkUrl(url);
			return v;
		}
		
	}